package org.rency.ues.annotation.support;

import org.apache.commons.lang.StringUtils;
import org.rency.common.utils.validate.RegValidator;
import org.rency.ues.common.UesDict;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: Created by user_rcy@163.com on 2016/8/3.
 */
public class AnnotationUtils {

    public static boolean isEmptyObject(Object arg) {
        if (arg == null) {
            return true;
        }
        if (arg instanceof Collection) {
            Collection<?> col = (Collection<?>) arg;
            if (col.isEmpty()) {
                return true;
            }
        } else if (arg.getClass().isArray()) {
            Object arr[] = (Object[]) arg;
            return arr.length <= 0;
        }
        return false;
    }

    public static boolean isCollection(Object arg) {
        if (arg == null) {
            return false;
        }
        if (arg instanceof Collection) {
            return true;
        } else if (arg.getClass().isArray()) {
            return true;
        }
        return false;
    }

    /**
     * 获取字段值
     * @param fieldName
     * @param param
     * @return
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     */
    public static Object getFieldValue(Object param, String fieldName) throws IllegalArgumentException, IllegalAccessException {
        Class<?> cls = param.getClass();
        for (; !cls.equals(Object.class); cls = cls.getSuperclass()) {
            try {
                Field field = cls.getDeclaredField(fieldName);
                field.setAccessible(true);
                return field.get(param);
            } catch (NoSuchFieldException e) {

            }
        }
        return null;
    }

    /**
     * 设置字段值
     * @param param
     * @param fieldName
     * @param value
     * @return
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static Object setFieldValue(Object param, String fieldName, Object value) throws IllegalArgumentException, IllegalAccessException {
        Class<?> cls = param.getClass();
        for (; !cls.equals(Object.class); cls = cls.getSuperclass()) {
            try {
                Field field = cls.getDeclaredField(fieldName);
                field.setAccessible(true);
                field.set(param, value);
                return param;
            } catch (NoSuchFieldException e) {

            }
        }
        return param;
    }

    /**
     * 计算字段摘要
     * @param summaryRule
     * @param data
     * @return
     */
    public static String getSummary(String summaryRule, String data) {
        if(StringUtils.isBlank(data) || StringUtils.isBlank(summaryRule)){
            return data;
        }
        if(RegValidator.isEmail(data)){
            return AnnotationUtils.getEmailMask(data,summaryRule);
        }else{
            return AnnotationUtils.getFiledMask(data,summaryRule);
        }
    }

    private static String getEmailMask(String email, String regex) {
        Pattern p = Pattern.compile(regex); // 正则表达式
        Matcher m = p.matcher(email); // 操作的字符串
        if (m.find()) {
            // email = email.replace(m.group(i), emailReplacement);
            email = getMatcherMask(m, email, UesDict.DEFAULT_MASK_EMAIL_CHAR);
        } else {
            p = Pattern.compile("@"); // 正则表达式
            m = p.matcher(email); // 操作的字符串
            StringBuffer sf = new StringBuffer();
            if (m.find()) {
                if (m.start() == 1) {
                    sf.append(UesDict.DEFAULT_MASK_EMAIL_CHAR);
                    sf.append(email.substring(m.end() - 1, email.length()));
                }
                if (m.start() == 2) {
                    sf.append(email.substring(0, 1));
                    sf.append(UesDict.DEFAULT_MASK_EMAIL_CHAR);
                    sf.append(email.substring(m.end() - 2, email.length()));

                }
            }
            if (null != sf && sf.length() > 0) {
                email = sf.toString();
            }
        }
        return email;
    }

    private static String getFiledMask(String filedValue, String regex) {
        Pattern p = Pattern.compile(regex); // 正则表达式
        Matcher m = p.matcher(filedValue); // 操作的字符串
        if (m.find()) {
            return getMatcherMask(m, filedValue, null);
        }
        return filedValue;
    }

    /**
     * 替换匹配的值为传入的值
     * @param m
     * @param filedValue
     * @param defaultReplaceValue
     * @return
     */
    private static String getMatcherMask(Matcher m, String filedValue, String defaultReplaceValue) {
        StringBuffer sf = new StringBuffer();
        for (int i = 1; i <= m.groupCount(); i++) {
            if (StringUtils.isBlank(defaultReplaceValue)) {
                defaultReplaceValue = getReplacement(m.group(i).length(), UesDict.DEFAULT_MASK_CHAR);
            }
            if (m.start(i) == 0) {
                sf.append(defaultReplaceValue);
                if (filedValue.length() > m.end(i)) {
                    sf.append(filedValue.substring(m.end(i), filedValue.length()));
                }
            } else {
                sf.append(filedValue.substring(0, m.start(i)));
                sf.append(defaultReplaceValue);
                if (filedValue.length() > m.end(i)) {
                    sf.append(filedValue.substring(m.end(i), filedValue.length()));
                }
            }
            if (null != sf && sf.length() > 0) {
                filedValue = sf.toString();
            }
        }
        return filedValue;
    }

    public static String getReplacement(int length, String replacement) {
        StringBuffer sf = new StringBuffer();
        for (int i = 0; i < length; i++) {
            sf.append(replacement);
        }
        return sf.toString();

    }

}